#!/usr/bin/python
import socket, traceback, os, sys, time, struct
import qutils

HTCP_WATER_SRV_IP    = '192.168.1.253'
HTCP_WATER_SRV_PORT  = 65520
HTCP_SERVER_TYPE_WATER  = 0x07
HTCP_WATER_CMD_START    = 0x79
HTCP_WATER_CMD_STOP     = 0x78
HTCP_WATER_CMD_STATUS   = 0x77

srvaddr = (HTCP_WATER_SRV_IP, HTCP_WATER_SRV_PORT)

def cmdSend(aCmd):
    try:
        qutils.rctLogger.info("REQ=%s" % (aCmd))
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(srvaddr)
        sock.send(aCmd)
        recv = sock.recv(1024)
        qutils.rctLogger.info("RSP=%s" % recv)
        return recv
    except socket.gaierror, e:
        print e
    except socket.error, e:
        print e
    return ''

def cmdStart(aUid, aDid) :
    print "-"*50
    print "CMD-START"
    tmpUid = str(aUid)
    tmpDid = str(aDid)
    tmpLen = len(tmpUid) + len(tmpDid)
    cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_START, tmpLen)
    cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
    cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
    recBuf = cmdSend(cmdBuf)
    if len(recBuf) == 9 :
        print "CMD-START-SUCCESS"
        return 0
    print "CMD-STOP-FAILURE"
    return -1
    
def cmdStop(aUid, aDid) :
    print "-"*50
    print "CMD-STOP"
    tmpUid = str(aUid)
    tmpDid = str(aDid)
    tmpLen = len(tmpUid) + len(tmpDid)
    #cmdBuf = struct.pack('!4sBBB16s', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STOP, tmpLen, tmpDid)
    cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STOP, tmpLen)
    cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
    cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
    recBuf = cmdSend(cmdBuf)
    if len(recBuf) == 9 :
        print "CMD-STOP-SUCCESS"
        return 0
    print "CMD-STOP-FAILURE"
    return -1
    
def cmdStatus(aUid, aDid) :
    print "-"*50
    print "CMD-STATUS"
    tmpUid = str(aUid)
    tmpDid = str(aDid)
    tmpLen = len(tmpUid) + len(tmpDid)
    cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STATUS, tmpLen)
    cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
    cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
    recBuf = cmdSend(cmdBuf)
    if len(recBuf) != 14 :
        print "CMD-STATUS-ERROR"
        return -1
    head,srv,act,len1,status,val,crc = struct.unpack("!4sBBBBfH", recBuf)
    print "status=%d,val=%f" % (status, val)
    print "CMD-STATUS-SUCCESS"
    return str(status) + "|" + str(val)

if __name__ == '__main__':
    print "**************************************************************************"
#    print "********************                                   *******************"
#    print "********************     WaterTest For Dev v1.0.0      *******************"
#    print "********************                                   *******************"
#    print "********************           Tommy@2016              *******************"
#    print "********************                                   *******************"
#    print "**************************************************************************"
    
    #astatus = cmdStart('24B5B806', '1234567890654777')
    #astatus = cmdStop('24B5B806', '1234567890654777')
    #astatus = cmdStatus('24B5B806', '5730303148FF6B067266545327')
    #astatus = cmdStatus('24B5B806', '5730303148FF70067266545313190667')
    #5730303148FF6F067266545332200667
    #5730303148FF70067266545313190667
    #5730303148FF6B067266545327200667
    astatus = cmdStatus('24B5B806', '5730303148FF6B067266545327200667')
    #astatus = cmdStart('24B5B806', '5730303148FF6B067266545327200667')
    #astatus = cmdStop('24B5B806', '5730303148FF6F067266545332200667')
    print astatus
    
